package com.readpost.service.impl;

import com.readpost.utils.ThreadLocalUtil;
import org.springframework.util.StringUtils;
import com.readpost.doman.Entity.User;
import com.readpost.mapper.UserMapper;
import com.readpost.service.UserService;
import com.readpost.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    /**
     * 根据id获取用户信息
     */
    @Override
    public User getUserById(int id) {
        return userMapper.getUserById(id);
    }

    /**
     * 用户登录
     */
    @Override
    public User userLogin(String username, String password) {
        String md5Password = Md5Util.md5(password);
        return userMapper.getUserByUsernameAndPassword(username,md5Password);
    }

    /**
     * 用户注册
     */
    @Override
    public void userRegister(String username, String password) {
        String md5Password = Md5Util.md5(password);
         userMapper.insertUser(username, md5Password);
    }

    /**
     * 根据用户名获取用户信息
     */
    @Override
    public User getUserByUsername(String user) {
        return userMapper.getUserByUsername(user);
    }

    /**
     * 更新用户信息
     *
     * @param user   用户信息
     * @param userid 用户id
     **/
    @Override
    public void updateUser(User user, Integer userid) {
        userMapper.updateUser(user, userid);
    }

    /**
     * 更新用户头像
     * @param avatarUrl 头像地址
     * @param userid    用户id
     */
    @Override
    public void updatePicture(String avatarUrl, Integer userid) {
        userMapper.updatePicture(avatarUrl, userid);
    }

    /**
     * 更新用户密码
     * @param pwdParams 密码参数
     */
    @Override
    public void updatePwd(Map<String,Object> pwdParams) {
        String oldPwd = (String) pwdParams.get("oldPwd");
        String newPwd = (String) pwdParams.get("newPwd");
        String rePwd = (String) pwdParams.get("rePwd");

        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            throw new RuntimeException("缺少参数");
        }
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer   userid = (Integer) map.get("userid");
        User user = userMapper.getUserById(userid);
        String md5Password = Md5Util.md5(oldPwd);
        if(user.getPasswordMd5().equals(md5Password)) {
            if (newPwd.equals(rePwd)) {
                String newMd5Password = Md5Util.md5(newPwd);
                userMapper.updatePwd(newMd5Password, userid);
            }else {
                throw new RuntimeException("两次密码不一致");
            }
        }else {
            throw new RuntimeException("原密码错误");
        }
    }

    //发送邮箱验证码
    @Override
    public void sendEmailCode(Map<String, Object> emailParams, Integer userid) {

    }

    //绑定邮箱
    @Override
    public void bindEmail(Map<String, Object> emailParams) {

    }
}
